package backend.majiong;

public class Pai {

    public static final Integer MIN_NUM = 0;

    public static final Integer TOTAL_NUM = 9;

    public static final Integer ZI_FENG_TOTAL_NUM = 4;

    public static final Integer ZI_TOTAL_NUM = 7;

    public static final Integer TYPE_SOU = 0;

    public static final Integer TYPE_PIN = 1;

    public static final Integer TYPE_WAN = 2;

    public static final Integer TYPE_ZI = 3;

    public static final Integer ZI_DONG = 0;

    public static final Integer ZI_NAN = 1;

    public static final Integer ZI_XI = 2;

    public static final Integer ZI_BEI = 3;

    public static final Integer ZI_BAI = 4;

    public static final Integer ZI_FA = 5;

    public static final Integer ZI_ZHONG = 6;

    private Integer id;

    private Integer type;

    private Integer num;

    private Boolean isRed;

    public Pai(Pai hai) {
        this.type = hai.type;
        this.num = hai.num;
        this.isRed = hai.isRed;
        this.id = hai.id;
    }

    public Pai(Integer type, Integer num, Boolean isRed, Integer id) {
        this.type = type;
        this.num = num;
        this.isRed = isRed;
        this.id = id;
    }

    public Pai(Integer num, Integer type) {
        this.type = type;
        this.num = num;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }

    public Boolean getIsRed() {
        return isRed;
    }

    public void setIsRed(Boolean isRed) {
        this.isRed = isRed;
    }

    /**
     * 是否字牌
     * 
     * @return
     */
    public Boolean isZiPai() {
        return this.type == TYPE_ZI;
    }

    /**
     * 是否三元牌
     * 
     * @return
     */
    public Boolean isSanYuan() {
        if (this.type != TYPE_ZI) {
            return false;
        }
        if (this.num >= ZI_BAI) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 得到下一张牌
     * 
     * @return
     */
    public Pai getNextDora() {
        Integer num = this.num;
        num++;
        Integer maxNum = 0;
        Integer minNum = 0;
        if (!this.isZiPai()) {
            maxNum = TOTAL_NUM - 1;
        } else if (this.isSanYuan()) {
            maxNum = ZI_TOTAL_NUM - 1;
            minNum = ZI_FENG_TOTAL_NUM;
        } else {
            maxNum = ZI_FENG_TOTAL_NUM - 1;
        }
        if (num > maxNum) {
            num = minNum;
        }
        Pai nextHai = new Pai(this);
        nextHai.setNum(num);
        return nextHai;
    }

    /**
     * 得到上一张牌
     * 
     * @return
     */
    public Pai getBeforeDora() {
        Integer num = this.num;
        num--;
        Integer maxNum = 0;
        Integer minNum = 0;
        if (!this.isZiPai()) {
            maxNum = TOTAL_NUM - 1;
        } else if (this.isSanYuan()) {
            maxNum = ZI_TOTAL_NUM - 1;
            minNum = ZI_FENG_TOTAL_NUM;
        } else {
            maxNum = ZI_FENG_TOTAL_NUM - 1;
        }
        if (num < minNum) {
            num = maxNum;
        }
        Pai nextHai = new Pai(this);
        nextHai.setNum(num);
        return nextHai;
    }

    public Pai getNextPai() {
        if (this.type.equals(TYPE_ZI)) {
            return null;
        }
        if (this.num.equals(9)) {
            return null;
        }
        return new Pai(this.num + 1, this.type);
    }

    public Object getBeforePai() {
        if (this.type.equals(TYPE_ZI)) {
            return null;
        }
        if (this.num.equals(1)) {
            return null;
        }
        return new Pai(this.num - 1, this.type);
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((num == null) ? 0 : num.hashCode());
        result = prime * result + ((type == null) ? 0 : type.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Pai other = (Pai) obj;
        if (num == null) {
            if (other.num != null)
                return false;
        } else if (!num.equals(other.num))
            return false;
        if (type == null) {
            if (other.type != null)
                return false;
        } else if (!type.equals(other.type))
            return false;
        return true;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public boolean isYaoJiu() {
        if (this.type.equals(TYPE_ZI) || this.num.equals(1) || this.num.equals(9)) {
            return true;
        }
        return false;
    }

    public boolean isYiJiu() {
        if (this.type.equals(TYPE_ZI)) {
            return false;
        }
        if (this.num.equals(1) || this.num.equals(9)) {
            return true;
        }
        return false;
    }

    public boolean isSiXi() {
        if (this.type != TYPE_ZI) {
            return false;
        }
        if (this.num < ZI_BAI) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public String toString() {
        switch (type) {
            case 0:
                return num + "s";
            case 1:
                return num + "p";
            case 2:
                return num + "m";
            case 3:
                return num + "z";
        }
        return "Pai [type=" + type + ", num=" + num + "]";
    }

}
